package com.cn.wanxi.util;

import java.io.*;
import java.util.List;

public class IO {

    /**
     * 创建文件夹和文件
     * @param type
     * @param fineName
     * @return
     */
    private static File createFile(String type, String fineName) {
        File file = new File(CommonPath.PATH.getValue());
        if (!file.exists()) {
            file.mkdirs();
            /**
             * mkdirs和mkdir区别
             *  \n\r
             */
        }
        File file1 = new File(file, fineName);
        if (type.equals("write")) {
            try {
                if (!file1.isFile())
                    file1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return file1;
    }

    public static void tempWrite(List<String> a) {
        File file = IO.createFile("write", CommonPath.COMPANY_NAME.getValue());
        try {
            OutputStream outputStream = new FileOutputStream(file);//输出流
            for (int i = 0; i < a.size(); i++) {
                outputStream.write((a.get(i)).getBytes());
                outputStream.write(CommonPath.AA.getValue().getBytes());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readNote(String fileName) {
        File file = IO.createFile("read", fileName);
        StringBuilder result = new StringBuilder();
        if (file.exists()) {
            try {
                Reader reader = new FileReader(file);
                int data;
                while ((data = reader.read()) > 0) {
                    result.append((char) data);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }
    /**
     * 1.封装model--字符串转换位数组  split
     * 2.遍历数组，forEach  for
     * 3.集合写，读，封装
     *  1）数组转换位字符串 Arrays.toString
     *  2）替换 replace
     *  3）枚举，封装（多余两次重复以上的需要提取公共部分）
     *  4）IO流注意事项：字节流和字符流   乱码
     *  5）缓冲流  buffer
     */
}
